package com.lemon.reggie.config;

import com.lemon.reggie.common.JacksonObjectMapper;
import com.lemon.reggie.interceptor.LoginInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    //@Resource
    //private LoginInterceptor loginInterceptor;

    /*@Override
    protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }*/



    //SpringBoot默认静态资源放在resources的static下
    //自定义设置静态资源映射
    /**
     * 设置静态资源映射
     * @param registry
     * @return void
     * @author Guannankun
     * @since 2022/5/23
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        log.info("开启静态资源映射...");
        //设置swagger静态资源映射
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        registry.addResourceHandler("/backend/**")  //静态资源对外访问的路径
                .addResourceLocations("classpath:/backend/"); //静态资源在本地的真实路径
        registry.addResourceHandler("/front/**")
                .addResourceLocations("classpath:/front/");
    }

    @Override
    protected void addInterceptors(InterceptorRegistry registry) {
        /*registry.addInterceptor(loginInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns("/backend/api/**", "/backend/images/**", "/backend/js/**",
                        "/backend/page/login/login.html", "/backend/plugins/**", "/backend/styles" +
                                "/**", "/backend/favicon.ico", "/api/**", "/images/**", "/js/**",
                        "/plugins/**", "/styles/**", "/front/**");*/
    }

    /**
     * 自定义扩展消息转换器
     * @param converters
     * @return void
     * @author Guannankun
     * @since 2022/5/26
     */
    @Override
    protected void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter messageConverter = new MappingJackson2HttpMessageConverter();
        messageConverter.setObjectMapper(new JacksonObjectMapper());
        //将自定义的扩展器放在首要位置执行
        converters.add(0, messageConverter);
    }
}
